package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.param.QuestionSearchParam;
import com.jbit.examSystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping("findAll")
    @ResponseBody
    public ResponseResult findAll(){
        List<Course> courses = courseService.getAllCourse();
        ResponseResult result = ResponseResult.success(courses);

        return result;
    }
    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(CourseCondition courseCondition, PageParam page){
        TableDataInfo respData = new TableDataInfo();
        if(courseCondition.gradeId==null){
            return respData;
        }

        try {
            PageHelper.startPage(page.getPageNum(), page.getPageSize(), true, true, false);
            List<Chapter> characters = courseService.getCourseByName(courseCondition.getCourseId());
            respData.succeed(characters, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }

        return respData;
    }

    @RequestMapping("getCourseByGradeId")
    @ResponseBody
    public List<Course> getCourseByGradeId(Integer gradeId){
        List<Course> courses = courseService.getCourseByGradeId(gradeId);
        return courses;
    }
}
